package com.globant;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.globant.profy.model.Languages;
import com.globant.profy.model.Permisos;
import com.globant.profy.model.User;

public class UserDAOImplementation extends HibernateDaoSupport implements
		UserDao {

	public UserDAOImplementation() {
	}

	/**
	 * Inserts an user in the data base
	 * 
	 * 
	 * @param User
	 *            : the user you want to insert
	 */
	public void createUser(User user) {
		user.setEnabled(1);
		user.setPermission(this.getPermission(user.getPermission().getTipo()));
		List<Languages> langs=new ArrayList<Languages>();
		for(int i=0;i<user.getLanguages().size();i++){
			langs.add(this.getLanguages(user.getLanguages().get(i).getIdioma()));
		}
		user.setLanguages(langs);
		getSession().close();
		getHibernateTemplate().save(user);
		
	}

	/**
	 * Updates an user in the data base
	 * 
	 * 
	 * @param User
	 *            : the user you want to update
	 */
	 public void updateUser(User user) {
		 List<Languages> langs=new ArrayList<Languages>();
		 for(int i=0;i<user.getLanguages().size();i++){
		 langs.add(this.getLanguages(user.getLanguages().get(i).getIdioma()));
		 }
		 user.setLanguages(langs);
		 getHibernateTemplate().update(user);

		 }

	
	public Permisos getPermission(String tipo){
		
		 Query query=getSession().createQuery("FROM Permisos u where u.tipo = :tipo");
		 query.setString("tipo", tipo);
		 getSession().close();
		 return (Permisos) query.uniqueResult();
		
	}
	public Languages getLanguages(String idioma){
		
		 Query query=getSession().createQuery("FROM Languages u where u.idioma = :idioma");
		 query.setString("idioma", idioma);
		 getSession().close();
		 return (Languages) query.uniqueResult();
		
	}

	/**
	 * Deletes an user in the data base
	 * 
	 * 
	 * @param User
	 *            : the user you want to delete
	 */
		public void deleteUser(User user) {
		getHibernateTemplate().delete(user);
	}

	/**
	 * Gets all users existing in the data base
	 * 
	 * 
	 * @returns List<User>: List of all the users in the data base
	 */
	public List<User> getAllUsers() {
		List<User> users = new ArrayList<User>();
		List<User> allUsers = getHibernateTemplate().loadAll(User.class);
		User user = new User();
		for (int i = 0; i < allUsers.size(); i++) {
			user = allUsers.get(i);
			if ((user.getEnabled() == 1)
					& ("ROLE_USER".equals(user.getPermission().getTipo()))) {
				users.add(allUsers.get(i));
			}
		}
		return users;
	}

	/**
	 * Searches the email given in the data base and gets the unique user
	 * corresponding to it
	 * 
	 * @param String
	 *            : Email you want to search
	 * @returns User: User corresponding to the email given
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public User getUserByEmail(final String email) {
		return (User) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) {
				getSession().close();
				Query query = getSession().createQuery(
						"FROM User u where u.email = :email");
				query.setString("email", email);
				return (User) query.uniqueResult();

			}
		});
	}

	// public User getUserByEmail(final String email) {
	// Query query =
	// getSession().createQuery("FROM User u where u.email = :email");
	// query.setString("email", email);
	// getSession().close();
	// return (User) query.uniqueResult();
	//
	//
	// }

	/**
	 * Searches the data given in the data base and gets the list of users
	 * corresponding to it
	 * 
	 * @param String
	 *            : info you want to search (looks for company, name and
	 *            experience)
	 * @returns List<User>: List of users that matches the info given
	 */
	@SuppressWarnings("unchecked")
	public List<User> getUserByCriteria(final String info) {
		List<User> users = new ArrayList<User>();
		User user;
if (info.equals("")){
	users=getAllUsers();
}else{
		Query query = getSession().createQuery(
				"FROM User c where c.company LIKE '%" + info
						+ "%' or c.name LIKE '%" + info
						+ "%' or c.experience LIKE '%" + info + "%'");
		users = (query.list());

		getSession().close();

		for (int i = 0; i < users.size(); i++) {
			user = users.get(i);
			if (user.getEnabled() == 0
					|| "ROLE_ADMIN".equals(user.getPermission().getTipo())) {
				users.remove(i);
			}
		}
}
		return users;

	}
}